﻿
@{
    ViewBag.Title = "Type";
    Layout = "~/Areas/Manage/Views/Shared/_SubPage.cshtml";
}
@section Header{}
@section Footer{    
    <script type="text/javascript">
    
        var ejApp = angular.module('ejapp', ['angular-icheck']);
        ejApp.controller('mainCtrl', ['$scope', '$http', function ($scope, $http) {
            var idStr = 'Id';                       //Id的字符串

            var updateUrl = Backstage+ "/Coupon/UpdateType";
            var updatesUrl =Backstage+ "/Coupon/UpdatesType";
            var removeUrl=Backstage+ "/Coupon/RemoveType";
            var removesUrl=Backstage+ "/Coupon/RemovesType";

            $scope.subject = '商品一级类型';                    //当前页的主题名称
            $scope.sIndex = -1;                         //当前选中的行号
            $scope.eModel = {};                         //当前编辑的对象模型
            $scope.datas =@MvcHtmlString.Create(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.Result));
            $scope.pIndex = 1;                          //当前显示的页索引
            $scope.query = {};                          //查询条件对象
            $scope.onSubmit = false;                    //是否提交   
   
   
            $scope.doSelect = function (data, e) { if (data.length > 0) { data.forEach(function (o) { o.Checked = e; }); } }
            $scope.doReSelect = function (data) { if (data.length > 0) { data.forEach(function (o) { o.Checked = !o.Checked; }); } }
            $scope.doUpdate = function (o) {            //编辑
                $scope.sIndex = $scope.datas.indexOf(o);
                $scope.onSubmit = false;
                $scope.eModel = angular.copy(o);             
                $('#modal-edit').modal();
            }
            $scope.save = function (valid) {
                $scope.onSubmit = true;
                if (!valid) return;
                showLoading();                          
                $http.post(updateUrl, {"model":$scope.eModel})
                    .success(function (rsp) {
                        if (rsp.Success) {
                            EJ.showSuccess("保存成功!");                                                  
                            $scope.datas[$scope.sIndex] = $scope.eModel;
                            $('#modal-edit').modal('hide');
                        } else {
                            EJ.showError(rsp.Message);
                        }
                        hideLoading();
                    })
                    .error(errorFn)
            }
            $scope.doRemove = function (o) {                //删除指定对象
                EJ.confimWarn('确定要删除指定的' + $scope.subject + '吗?', function () {
                    $http.post(removeUrl, { "id": o[idStr] })
                    .success(function (rsp) {
                        if (rsp.Success) {
                            EJ.showSuccess("删除成功");
                            $scope.datas.remove(o);                      
                        } else {
                            EJ.showError(rsp.Message);
                        }
                    })
                    .error(errorFn);
                });
            }
            $scope.doRemoves = function () {                //批量删除选择对象
                if ($scope.datas.length == 0) return;
                var ids = []; $scope.datas.forEach(function (o) { if (o.Checked) { ids.push(o[idStr]); } });
                var length = ids.length;
                if (length > 0) {
                    EJ.confimWarn('确定要删除选中的' + length + '条' + $scope.subject + '记录吗?', function () {
                        $http.post(removesUrl, { "ids": ids })
                        .success(function (rsp) {
                            if (rsp.Success) {
                                EJ.showSuccess("删除成功");                       
                                $scope.datas = $scope.datas.filter(function (o) { return o.Checked == undefined || o.Checked == false; });
                            } else {
                                EJ.showError(rsp.Message);
                            }
                        })
                        .error(errorFn);
                    });
                } else {
                    EJ.toastrInfo("请选择要操作的" + $scope.subject + "！");
                }
            }
            $scope.doUpdates = function (p, v) { //批量编辑选择对象
                if ($scope.datas.length == 0) return;
                var ids = [];
                $scope.datas.forEach(function (o) { if (o.Checked) { ids.push(o[idStr]); } });
                var length = ids.length;
                if (length > 0) {
                    EJ.confimWarn('确定要批量编辑选中的' + length + '条' + $scope.subject + '记录吗?', function () {
                        $http.post(updatesUrl, { "Ids": ids, "Property": p, "Value": v })
                        .success(function (rsp) {
                            if (rsp.Success) {
                                EJ.showSuccess("编辑成功");
                                $scope.datas.forEach(function (item) { if (item.Checked == true && item[p] != v) { item[p] = v; } });
                            } else {
                                EJ.showError(rsp.Message);
                            }
                        })
                        .error(errorFn);
                    });
                } else {
                    EJ.toastrInfo("请选择要操作的" + $scope.subject + "！");
                }
            }              
        }]);
    </script>
}
<div class="wrapper wrapper-content animated fadeInUp">
    <div class="row  animated slideInRight">
        <div class="col-sm-12">
            <div class="ibox">
                <div class="ibox-title">
                    <h5><i class="fa fa-database"></i>&nbsp;&nbsp;{{subject}}管理</h5>
                    <div class="ibox-tools">                      
                        <button type="button" ng-click="doRemoves()" class="btn btn-danger btn-xs">删除&nbsp;<i class="fa fa-trash-o"></i></button>
                        <div class="btn-group">
                            <button type="button" data-toggle="dropdown" class="btn btn-primary btn-xs dropdown-toggle">状态操作 &nbsp;<span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu">
                                <li>
                                    <a ng-click="doUpdates('IsShow',true)">全部显示</a>
                                </li>
                                <li>
                                    <a ng-click="doUpdates('IsShow',false)">全部隐藏</a>
                                </li>                                                              
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="ibox-content">
                    <table class="table table-hover table-striped table-bordered table-advanced tablesorter" id="data_container">
                        <thead>
                            <tr>
                                <th class="text-center" width="30px">
                                    <div class="btn-group">
                                        <button data-toggle="dropdown" class="btn btn-primary btn-xs dropdown-toggle"><span class="caret"></span></button>
                                        <ul class="dropdown-menu dropdown-menu-right">
                                            <li>
                                                <a ng-click="doSelect(datas,true)">全选</a>
                                            </li>
                                            <li>
                                                <a ng-click="doSelect(datas,false)">全不选</a>
                                            </li>
                                            <li class="divider"></li>
                                            <li>
                                                <a ng-click="doReSelect(datas)">反选</a>
                                            </li>
                                        </ul>
                                    </div>
                                </th>                          
                                <th class="text-center">名称</th>
                                <th class="text-center">排序</th>
                                <th class="text-center">状态</th>                           
                                <th class="text-center">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="item in datas">
                                <td class="text-center"><input type='checkbox' icheck ng-model="item.Checked" /></td>                            
                                <td class="text-center" ng-bind="item.Name"></td>
                                <td class="text-center" ng-bind="item.DisplayIndex"></td>                         
                                <td class="text-center">
                                    <span class='label label-sm label-success' ng-show="item.IsShow">显示</span>
                                    <span class='label label-sm label-danger' ng-show="!item.IsShow">隐藏</span>
                                </td>                            
                                <td class="text-center">
                                    <button type='button' class='btn btn-info btn-xs' ng-click='doUpdate(item)'><i class='fa fa-edit'></i>&nbsp;编辑</button>&nbsp;
                                    <button type='button' class='btn btn-danger btn-xs' ng-click='doRemove(item)'><i class='fa fa-trash-o'></i>&nbsp;删除</button>
                                </td>
                            </tr>
                        </tbody>
                        <tfoot ng-show="tModels==0">
                            <tr>
                                <td colspan="6"><p class="text-center">没有找到记录.</p></td>
                            </tr>
                        </tfoot>
                    </table>                 
                </div>
            </div>
        </div>
    </div>
</div>
<div id="modal-edit" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="modal-stackable-label" aria-hidden="true" class="modal fade">
    <div class="modal-dialog  modal-lg">
        <form name="eForm" ng-submit="save(eForm.$valid)" class="form-horizontal form-bordered form-row-stripped" role="form" novalidate>
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" data-dismiss="modal" aria-hidden="true" class="close">&times;</button>
                    <h4 id="modal-stackable-label" class="modal-title">{{subject}}-编辑</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-sm-12" ng-class="{'state-error':eForm.Name.$invalid&&onSubmit}">
                            <label class="control-label">名称:<span class="require">*</span></label>
                            <input type="text" name="Name" ng-model="eModel.Name" ng-required="true" class="form-control" />
                            <em ng-show="eForm.Name.$invalid&&onSubmit" class="invalid">必填字段!</em>
                        </div>
                    </div>                 
                    <div class="row">
                        <div class="col-sm-3">
                            <p class="m-t">
                                <input id="cbIsShow" type="checkbox" icheck value="true" ng-model="eModel.IsShow" />
                                <label for='cbIsShow'>显示</label>
                            </p>
                        </div>                                           
                    </div>
                    <div class="col-sm-3" ng-class="{'state-error':eForm.DisplayIndex.$invalid&&onSubmit}">
                        <label class="control-label">排序:<span class="require">*</span></label>
                        <input type="number" name="DisplayIndex" ng-model="eModel.DisplayIndex" ng-required="true" class="form-control" />
                        <em ng-show="eForm.DisplayIndex.$invalid&&onSubmit" class="invalid">必填字段!</em>
                    </div>                                                                       
                </div>
                <div class="modal-footer">
                    <button type="submit" id="btnSave" class="btn btn-primary">保存</button>
                    <button type="button" data-dismiss="modal" class="btn btn-default">关闭</button>
                </div>
            </div>
        </form>
    </div>
</div>

